package models

import (
	"fly/schema"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
)

// AircraftLocation 机位
type AircraftLocation struct {
	BaseModel
	Code string `orm:"column(code);description(代码)" json:"code"`
	Type int    `orm:"column(type);description(机位类型)" json:"type"`
	No   int    `orm:"column(no);description(编号)" json:"no"`
}

func init() {
	orm.RegisterModel(new(AircraftLocation))
}

// SimpleField 简略查询字段
func (*AircraftLocation) SimpleField() []string {
	return []string{"id", "code", "type", "no"}
}

// SearchField 定义模型的可搜索字段
func (*AircraftLocation) SearchField() []string {
	return []string{"code", "status", "type", "no"}
}

// WhereField 定义模型可作为条件的字段
func (*AircraftLocation) WhereField() []string {
	return []string{"id", "status", "type", "code"}
}

func (m *AircraftLocation) TableName() string {
	return "aircraft_location"
}

// AddAircraftLocation insert a new AircraftLocation into database and returns
// last inserted Id on success.
func AddAircraftLocation(m *AircraftLocation, userId int64) (id int64, err error) {
	o := orm.NewOrm()
	m.Creator = userId
	m.Updater = userId
	id, err = o.Insert(m)
	return
}

// GetAircraftLocationById retrieves AircraftLocation by Id. Returns error if
// Id doesn't exist
func GetAircraftLocationById(id int64) (v *AircraftLocation, err error) {
	o := orm.NewOrm()
	v = &AircraftLocation{BaseModel: BaseModel{Id: id}}
	if err = o.QueryTable(new(AircraftLocation)).Filter("id", id).RelatedSel().One(v); err == nil {
		return v, nil
	}
	return nil, err
}

// UpdateAircraftLocationById updates AircraftLocation by Id and returns error if
// the record to be updated doesn't exist
func UpdateAircraftLocationById(m *AircraftLocation, userId int64) (err error) {
	o := orm.NewOrm()
	v := AircraftLocation{BaseModel: BaseModel{Id: m.Id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		m.Updater = userId
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteAircraftLocation deletes AircraftLocation by Id and returns error if
// the record to be deleted doesn't exist
func DeleteAircraftLocation(id int64) (err error) {
	o := orm.NewOrm()
	v := AircraftLocation{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&AircraftLocation{BaseModel: BaseModel{Id: id}}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

// DestroyAircraftLocation Destroy AircraftLocation by Id and returns error if
func DestroyAircraftLocation(id int64, userId int64) (err error) {
	o := orm.NewOrm()
	v := AircraftLocation{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		v.Status = schema.StatusDelete
		v.Updater = userId
		var num int64
		if num, err = o.Update(v, "updater", "status"); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}
